Completed
Push — master ( 0c15fd...9d8885 )
by Wallace
02:03
created

database.js ➔ ... ➔ proto.getDatabase   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
/**
2
 * Database
3
 */
4
'use strict'
5
6
const fs = require('fs')
7
const _ = require('lodash')
8
9
const Database = () => {
10
  const __ = {
11
    getDatabase: database => {
12
      if (!fs.existsSync(database)) {
13
        return {}
14
      }
15
16
      return JSON.parse(fs.readFileSync(database, 'utf8'))
17
    }
18
  }
19
20
  const proto = {
21
    data: '',
22
    database: '',
23
24
    /**
25
     * Set file database.
26
     *
27
     * @param {string} pathname
28
     */
29
    setFile: function (pathname) {
30
      let regex = new RegExp(/\.json$/)
31
32
      if (!regex.test(pathname)) {
33
        throw new Error('The database file must be a json file')
34
      }
35
36
      this.database = pathname
37
38
      return this
39
    },
40
41
    /**
42
     * Add a key and value for database.
43
     *
44
     * @param {string} key
45
     * @param {string} value
46
     */
47
    add: function (key, value) {
48
      this.data = __.getDatabase(this.database)
49
      this.data[key] = value
50
51
      return this
52
    },
53
54
    /**
55
     * Add values to env vars from node.
56
     *
57
     * @param {object} data
58
     */
59
    addEnv: function (data) {
60
      if (typeof data !== 'object') {
61
        throw new Error('data must be an object')
62
      }
63
64
      _.map(data, (value, key) => {
65
        process.env[key] = value
66
      })
67
    },
68
69
    /**
70
     * Add multiples keys to database.
71
     *
72
     * @param {object} object
0 ignored issues
show
Documentation introduced by
The parameter object does not exist. Did you maybe forget to remove this comment?
Loading history...
73
     */
74
    massive: function (data) {
75
      if (typeof data !== 'object') {
76
        throw new Error('data must be an object')
77
      }
78
79
      _.map(data, (value, key) => this.add(key, value))
80
81
      return this
82
    },
83
84
    /**
85
     * Return an key from database or all data.
86
     *
87
     * @param {string} item
88
     */
89
    get: function (item) {
90
      let db = __.getDatabase(this.database)
91
92
      if (item !== undefined && !(item in db)) {
93
        throw new Error(`${item} is undefined`)
94
      }
95
96
      return db[item] || db
97
    },
98
99
    /**
100
     * Store data to db.json.
101
     */
102
    store: function () {
103
      fs.writeFile(this.database, JSON.stringify(this.data), err => {
104
        if (err) {
105
          throw new Error(err)
106
        }
107
108
        return
0 ignored issues
show
Unused Code introduced by
This return has no effect and can be removed.
Loading history...
109
      })
110
    }
111
  }
112
113
  return Object.create(proto)
114
}
115
116
module.exports = Database()
117